package com.miku.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import com.miku.common.BaseController;
import com.miku.sys.config.Global;
import com.miku.sys.security.realm.SystemAuthorizingRealm.Principal;
import com.miku.sys.utils.UserUtils;
import com.miku.utils.StringUtils;
import com.miku.utils.UserAgentUtils;

import eu.bitwalker.useragentutils.UserAgent;

@Transactional
@Controller
public class SystemController extends BaseController {
	private static String errIP = null;//异常的ip
	
	private String getHome(HttpServletRequest request){
		if(UserAgentUtils.isMobileOrTablet(request)){
			return "forward:" + Global.getWechatRoot() +Global.getWechatHome();
		}else{
			return "forward:" + Global.getWebRoot() + Global.getWebHome();
		}
		/*return "forward:" + Global.getWebRoot() + Global.getWebHome();*/
	}
	
	private String getLogin(HttpServletRequest request){
		if(UserAgentUtils.isMobileOrTablet(request)){
			return "forward:" + Global.getWechatRoot() + "/api/loginAuto";
		}else{
			return "forward:" + Global.getWebRoot() + "/login";
		}
		/*return "forward:" + Global.getWebRoot() + Global.getWebHome();*/
	}
	
	//访问根目录的时候
	@RequestMapping("/")
	public String defaultHome(HttpServletRequest request){
		//return getHome(request);
		return "redirect:/web/login";
	}
	
	@RequestMapping("${web.url.root}")
	public String webHome(HttpServletRequest request){
		return getHome(request);
	}
	
	@RequestMapping("${wechat.url.root}")
	public String wechatHome(HttpServletRequest request){
		return getHome(request);
	}
	
	@RequestMapping("/sys/err/400")
	public String sysErr400(HttpServletRequest request){
		String forwardURI = (String) request.getAttribute("javax.servlet.forward.request_uri");
		logger.info("400 error, url = " + forwardURI);
		return "/error/400";
	}
	
	@RequestMapping("/sys/err/401")
	public String sysErr401(HttpServletRequest request){
		String forwardURI = (String) request.getAttribute("javax.servlet.forward.request_uri");
		logger.info("401 error, url = " + forwardURI);
		return "/error/401";
	}
	
	@RequestMapping("/sys/err/403")
	public String sysErr403(HttpServletRequest request){
		String forwardURI = (String) request.getAttribute("javax.servlet.forward.request_uri");
		Principal p = UserUtils.getPrincipal();
		logger.info("403 error, url = " + forwardURI);
		return "/error/403";
	}
	
	@RequestMapping("/sys/err/404")
	public String sysErr404(HttpServletRequest request, HttpServletResponse resp){
		String forwardURI = (String) request.getAttribute("javax.servlet.forward.request_uri");
		String fromIP = StringUtils.getRemoteAddr(request);
		
		if(forwardURI != null && (forwardURI.endsWith(".php") || forwardURI.endsWith(".cgi")) || (errIP != null && errIP.equals(fromIP))){
			try {
				errIP = fromIP;
				logger.debug("访问来自ip：" + errIP + "，url = " + forwardURI);
				UserAgent u = UserAgentUtils.getUserAgent(request);
				if(u != null){
					logger.debug("异常访问者信息： " + u.toString());
				}
				resp.getWriter().write("get out.");
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "redirect:http://www.baidu.com/s?wd=get%20out";
		}else{
			logger.info("404 error,  url = " + forwardURI);
			return "/error/404";
		}
		//如果是手机或者平板则返回微信页面，暂时这么定义
		//return "/error/404";//"redirect:" + Global.getWebRoot();
	}
	
	@RequestMapping("/sys/err/500")
	public String sysErr500(HttpServletRequest request, HttpServletResponse resp){
		String forwardURI = (String) request.getAttribute("javax.servlet.forward.request_uri");
		logger.info("500 error, url = " + forwardURI);
		return "/error/500";
	}
	
	@RequestMapping("/sys/login")
	public String sysLogin(HttpServletRequest request){
		String forwardURI = request.getRequestURL().toString();
		logger.debug("访问login页面...forwardURI = " + forwardURI);
		return getLogin(request);
	}
}